﻿var Index = 0;

function ClientAddDocument() {
    belongSite = document.getElementById("drpSelectSite").options[document.getElementById("drpSelectSite").selectedIndex].value;
    Url = Portal.CMS.Document.GetAddNewUrl(belongSite).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }

    window.open(Url, '_blank');
   // FilterFunc();
}

function SaveClientAddDocument() {
    belongSite = document.getElementById("txtBelongSite").value;
    Subject = document.getElementById("txtSubject").value;
    Description = document.getElementById("txtDescription").value;
    Url = document.getElementById("txtUrl").value;
    retValue = Portal.CMS.AddDocument.ServerSideAddDocument(belongSite, Subject, Description, Url).value;
    if (retValue.indexOf("ERROR") != -1) {
        alert(retValue);
        return;
    }
    alert(retValue);
    window.close();
}


function ClientDeleteDocument(DocumentId) {
    retValue = Portal.CMS.Document.ServerSideCheckAllowDelete(DocumentId).value;
    if (retValue.indexOf("ERROR") != -1) {
        alert(retValue);
        return;
    }

    Msg = Portal.CMS.Document.GetConfirmDeleteMsg().value;
    if (confirm(Msg) == false) return;
    RetValue = Portal.CMS.Document.ServerSideDeleteDocument(DocumentId).value;
    if (RetValue.indexOf("ERROR") != -1) {
        alert(RetValue);
        return;
    }
    alert(RetValue);
    FilterFunc();
}

function ShowClientUpdateUrlForm(DocumentId) {
    Url = Portal.CMS.Document.GetShowUpdateForm(DocumentId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    window.open(Url, '_blank');
    //FilterFunc();
}

function ShowFormAppendLink(DocumentId) {
    Url = Portal.CMS.UpdateDocument.GetShowAppendForm(DocumentId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    window.open(Url, '_blank');
//    value = Portal.CMS.UpdateDocument.GetHtml(DocumentId).value;
//    if (value.indexOf("ERROR") == -1) {
//        document.getElementById("divFormContent").innerHTML = value;
//    }
}

function ClientSaveUpdateDocument(DocumentId) {
    Subject = document.getElementById("txtDocument").value;
    Description = document.getElementById("txtDescription").value;
    Url = document.getElementById("txtUrl").value;
    language = document.getElementById("divSelectLanguage").options[document.getElementById("divSelectLanguage").selectedIndex].value;

    RetValue = Portal.CMS.UpdateDocument.ServerSideUpdateDocument(language, DocumentId, Subject, Description, Url).value;
    if (RetValue.indexOf("ERROR") != -1) {
        alert(RetValue);
        return;
    }
    alert(RetValue);
    //window.close();
}

function ClientSaveDocumentItems(DocumentId, MaxItems) {
    //Subject=document.getElementById("txtDocument").value;
    //Description=document.getElementById("txtDescription").value;
    DocumentItems = Portal.CMS.DocumentItems.GetDocumentItemsObject(MaxItems).value;
    for (var Index = 0; Index < DocumentItems.length; Index++) {
        if (document.getElementById("txtItemName_" + Index.toString()) != null) {
            DocumentItems[Index].DocumentItemId = document.getElementById("txtItemId_" + Index.toString()).value;
            DocumentItems[Index].DocumentItemName = document.getElementById("txtItemName_" + Index.toString()).value;
            DocumentItems[Index].Url = document.getElementById("txtItemUrl_" + Index.toString()).value;
            DocumentItems[Index].Target = document.getElementById("txtItemTarget_" + Index.toString()).value;
            DocumentItems[Index].Description = document.getElementById("txtItemDescription_" + Index.toString()).value;
            DocumentItems[Index].IndexOrder = document.getElementById("txtItemIndexOrder_" + Index.toString()).value;
        }
    }
    language = document.getElementById("divSelectLanguage").options[document.getElementById("divSelectLanguage").selectedIndex].value;

    RetValue = Portal.CMS.DocumentItems.ServerSideSaveDocumentItems(language, DocumentId, DocumentItems).value;
    if (RetValue.indexOf("ERROR") != -1) {
        alert(RetValue);
        return;
    }
    alert(RetValue);
    //window.close();
}



function AppendNewRow() {
    document.getElementById("divContentItems").innerHTML = document.getElementById("divContentItems").innerHTML + Portal.CMS.AddDocument.ServerSideNewLinkItem(Index).value;
    Index++;
}

function RemoveItem(Index) {
    document.getElementById("divRowItem_" + Index).innerHTML = "";
    Html = document.getElementById("divContentItems").innerHTML;
    document.getElementById("divContentItems").innerHTML = Portal.CMS.AddDocument.ProcessHtml(Html, Index).value;
}


function ShowPermissionFuncForm(DocumentId) {
    Url = Portal.CMS.Document.GetShowFormSetPermission(DocumentId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    window.open(Url, '_blank');
}

function ShowAddToAlbumForm(DocumentId) {
    Url = Portal.CMS.Document.GetAddToAlbumForm(DocumentId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    window.open(Url, '_blank');
}


function ClientAdd2Album(DocumentId) {
    AlbumId = document.getElementById("drpSelectAlbum").options[document.getElementById("drpSelectAlbum").selectedIndex].value;
    retValue = Portal.CMS.AddToAlbum.ServerSideAddToAlbum(DocumentId, AlbumId).value;
    if (retValue.indexOf("ERROR") != -1) {
        alert(retValue);
        return;
    }
    alert(retValue);
    window.close();
}

function DetectDocumentDescription(DocumentId) {
    language = document.getElementById("divSelectLanguage").options[document.getElementById("divSelectLanguage").selectedIndex].value;

    retValue = Portal.CMS.UpdateDocument.ServerGetDescription(language, DocumentId).value;
    document.getElementById("txtDescription").value = retValue;
}

function DeleteDocumentItem(DocumentId, DocumentItemId) {
    RetValue = Portal.CMS.DocumentItems.ServerSideCheckDeleteDocumentItem(DocumentItemId).value;
    if (RetValue.indexOf("ERROR") != -1) {
        alert(RetValue);
        return; 
    }
    Msg = Portal.CMS.DocumentItems.GetConfirmDeleteMsg().value;
    if (confirm(Msg) == false) return;
    RetValue = Portal.CMS.DocumentItems.ServerSideDeleteDocumentItem(DocumentItemId).value;
    if (RetValue.indexOf("ERROR") != -1) {
        alert(RetValue);
        return;
    }
    alert(RetValue);
    value = Portal.CMS.DocumentItems.GetHtml(DocumentId, "").value;
    if (value.indexOf("ERROR") == -1) {
        document.getElementById("divFormContent").innerHTML = value;
    }
}

function UploadNewImage(DocumentId) {
    Url = Portal.CMS.UpdateDocument.GetAddDocumentItemUrl(DocumentId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    window.open(Url, '_blank');
//    value = Portal.CMS.UpdateDocument.GetHtml(DocumentId).value;
//    if (value.indexOf("ERROR") == -1) {
//        document.getElementById("divFormContent").innerHTML = value;
//    }
}





function FilterFunc() {
    document.getElementById("divProcessing").innerHTML = Portal.CMS.Document.GetProcessingText().value;
    setTimeout("RealFilterFunc()", 10);
}


function RealFilterFunc() {
    belongSite = document.getElementById("drpSelectSite").options[document.getElementById("drpSelectSite").selectedIndex].value;
    retValue = Portal.CMS.Document.GetHtml(belongSite).value;
    if (retValue.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(retValue);
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    document.getElementById("divDocumentMain").innerHTML = retValue;
}


function ShowDocument(DocumentId) {
    Url = Portal.CMS.Document.GetDocumentUrl(DocumentId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    window.open(Url, "_blank");
}

function ClientReading(DocumentId) {
    document.getElementById("divProcessing").innerHTML = Portal.CMS.DocumentItems.GetProcessingText().value;
    setTimeout("RealClientReading('" + DocumentId + "')", 10);
}


function RealClientReading(DocumentId) {
    language = document.getElementById("divSelectLanguage").options[document.getElementById("divSelectLanguage").selectedIndex].value;
    Keyword = document.getElementById("txtKeyword").value;
    value = Portal.CMS.DocumentItems.GetHtml(language, DocumentId, Keyword, 0).value;
    if (value.indexOf("ERROR") == -1) {
        document.getElementById("divProcessing").innerHTML = "";
        document.getElementById("divFormContent").innerHTML = value;
    }
    document.getElementById("divProcessing").innerHTML = "";
}





function SaveDocumentItem(Index, DocumentId, DocumentItemId) {
    RetValue = Portal.CMS.DocumentItems.ServerSideCheckSaveDocumentItem(DocumentItemId).value;
    if (RetValue.indexOf("ERROR") != -1) {
        alert(RetValue);
        return;
    }

    document.getElementById("divProcessing").innerHTML = Portal.CMS.DocumentItems.GetProcessingText().value;
    setTimeout("RealSaveDocumentItem(" + Index + ",'" + DocumentId + "','" + DocumentItemId + "')", 10);
}


function RealSaveDocumentItem(Index, DocumentId, DocumentItemId) {
    DocumentItemName = document.getElementById("txtItemName_" + Index.toString()).value;
    Url = document.getElementById("txtItemUrl_" + Index.toString()).value;
    Target = document.getElementById("txtItemTarget_" + Index.toString()).value;
    Description = document.getElementById("txtItemDescription_" + Index.toString()).value;
    IndexOrder = document.getElementById("txtItemIndexOrder_" + Index.toString()).value;
    language = document.getElementById("divSelectLanguage").options[document.getElementById("divSelectLanguage").selectedIndex].value;

    RetValue = Portal.CMS.DocumentItems.ServerSideSaveDocumentItem(language, DocumentId, DocumentItemId, DocumentItemName, Url, Target, Description, IndexOrder).value;
    if (RetValue.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(RetValue);
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    alert(RetValue);
}



function UploadNewImageFromListItems(DocumentId) {
    Url = Portal.CMS.DocumentItems.GetAddDocumentItemUrl(DocumentId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    window.open(Url, '_blank');
//    value = Portal.CMS.DocumentItems.GetHtml(DocumentId, "").value;
//    if (value.indexOf("ERROR") == -1) {
//        document.getElementById("divFormContent").innerHTML = value;
//    }
}


function PermissionDocumentItem(DocumentItemId) {
    Url = Portal.CMS.DocumentItems.GetDocumentItemUrl(DocumentItemId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    window.open(Url, '_blank');
}





function ClientPublishing(DocumentItemId) {
    retValue = Portal.CMS.DocumentItems.ServerSideCheckPublishing(DocumentItemId).value;
    if (retValue.indexOf("ERROR") != -1) {
        alert(retValue);
        return;
    }
    ConfirMsg = Portal.CMS.DocumentItems.GetConfirmPublishingMsg().value;
    if (confirm(ConfirMsg) == false) return;
    retValue = Portal.CMS.DocumentItems.ServerSidePublishing(DocumentItemId).value;
    alert(retValue);
    if (retValue.indexOf("ERROR") != -1) {
        retValue = Portal.CMS.DocumentItems.GetUnPublishingCaption().value;
        Document.getElementById("txtPublishing_" + DocumentItemId).value = retValue;
    }
}


function ClientUnPublishing(DocumentItemId) {
    retValue = Portal.CMS.DocumentItems.ServerSideCheckUnPublishing(DocumentItemId).value;
    if (retValue.indexOf("ERROR") != -1) {
        alert(retValue);
        return;
    }
    ConfirMsg = Portal.CMS.DocumentItems.GetConfirmUnPublishingMsg().value;
    if (confirm(ConfirMsg) == false) return;
    retValue = Portal.CMS.DocumentItems.ServerSideUnPublishing(DocumentItemId).value;
    alert(retValue);
    if (retValue.indexOf("ERROR") != -1) {
        retValue = Portal.CMS.DocumentItems.GetPublishingCaption().value;
        Document.getElementById("txtPublishing_" + DocumentItemId).value = retValue;
    }
}


function DoSyncAll(DocumentId) {
    if (confirm(Portal.CMS.DocumentItems.GetConfirmSyncAllMsg().value) == false) {
        return;
    }
    document.getElementById("divProcessing").innerHTML = Portal.CMS.DocumentItems.GetSyncProcessing().value;
    setTimeout("RealDoSyncAll('" + DocumentId + "')", 10);
}

function RealDoSyncAll(DocumentId) {
    retValue = Portal.CMS.DocumentItems.SyncToServer(DocumentId).value;
    document.getElementById("divProcessing").innerHTML = "";
    alert(retValue);
}

function ClientJumpDocumentItem(TabModuleId, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    ActiveLanguage = document.getElementById("txtLanguage").value;
    DocumentId = document.getElementById("txtDocumentId").value;
    retValue = Portal.CMS.DocumentItems.GetHtml(ActiveLanguage, DocumentId, "", CurrentPage).value;
    document.getElementById("divFormContent").innerHTML = retValue;
}

function UploadNewDocumentItemForUpdate(DocumentId, DocumentItemId) {
    Url = Portal.CMS.DocumentItems.GetUploadUpdateImage(DocumentId, DocumentItemId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    window.open(Url, '_blank');
}


function ClientViewAttachFile(DocumentItemId) {
    document.getElementById("divProcessing").innerHTML = Portal.CMS.DocumentItems.GetProcessingText().value;
    setTimeout("RealClientViewAttachFile('"+DocumentItemId+"')", 10);
}

function RealClientViewAttachFile(DocumentItemId) {
    Url=Portal.CMS.DocumentItems.ServerSideDownloadItem(DocumentItemId).value;
    document.getElementById("divProcessing").innerHTML = "";
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    window.open(Url, '_blank');
}


function ClientSyncDocumentItem(DocumentItemId) {
    if (confirm(Portal.CMS.DocumentItems.GetConfirmSyncAllMsg().value) == false) {
        return;
    }
    document.getElementById("divProcessing").innerHTML = Portal.CMS.DocumentItems.GetProcessingText().value;
    setTimeout("RealClientSyncDocumentItem('" + DocumentItemId + "')", 10);
}

function RealClientSyncDocumentItem(DocumentItemId) {
    rValue = Portal.CMS.DocumentItems.ServerSideSyncDocumentItem(DocumentItemId).value;
    alert(rValue);
    document.getElementById("divProcessing").innerHTML = "";
}



function ClientSyncDocument(DocumentId) {
    if (confirm(Portal.CMS.Document.GetConfirmSyncAllMsg().value) == false) {
        return;
    }
    document.getElementById("divProcessing").innerHTML = Portal.CMS.Document.GetProcessingText().value;
    setTimeout("RealClientSyncDocument('" + DocumentId + "')", 10);
}

function RealClientSyncDocument(DocumentId) {
    rValue = Portal.CMS.Document.ServerSideSyncDocument(DocumentId).value;
    alert(rValue);
    document.getElementById("divProcessing").innerHTML = "";
}